package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.stylefeng.guns.api.user.UserAPI;
import com.stylefeng.guns.api.user.UserInfoModel;
import com.stylefeng.guns.api.user.UserModel;
import com.stylefeng.guns.rest.common.CurrentUser;
import com.stylefeng.guns.rest.modular.vo.ReponseVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user/")
@RestController
public class UserController {

    @Reference(interfaceClass = UserAPI.class, check = false)
    private UserAPI userAPI;

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public ReponseVo register(UserModel userModel) {

        if (userModel.getUsername() == null || userModel.getUsername().trim().length() == 0) {
            return ReponseVo.serviceFail("用户名不能为空");
        }

        if (userModel.getPassword() == null || userModel.getPassword().trim().length() == 0) {
            return ReponseVo.serviceFail("密码不能为空");
        }

        boolean isSuccess = userAPI.register(userModel);
        if (isSuccess) {
            return ReponseVo.success("注册成功");
        } else {
            return ReponseVo.serviceFail("注册失败");
        }
    }

    @RequestMapping(value = "check", method = RequestMethod.POST)
    public ReponseVo check(String username) {
        if (username != null && username.trim().length() > 0) {
            boolean notExists = userAPI.checkUserName(username);
            //返回ture时  用户名可以注册
            if (notExists) {
                return ReponseVo.success("用户名不存在");
            } else {
                return ReponseVo.serviceFail("用户名已存在");
            }
        } else {
            return ReponseVo.serviceFail("用户名不能为空");
        }
    }

    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public ReponseVo logout() {
        //前端需要删除jwt
        return ReponseVo.success("退出成功");
    }

    @RequestMapping(value = "getUserInfo", method = RequestMethod.GET)
    public ReponseVo getUserInfo() {
        //获取当前登录用户
        String userId = CurrentUser.getCurrentUserId();
        if (userId != null && userId.trim().length() > 0) {
            //将id查询返回
            int uuid = Integer.parseInt(userId);
            UserInfoModel userInfo = userAPI.getUserInfo(uuid);
            if (userInfo != null) {
                return ReponseVo.success(userInfo);
            } else {
                return ReponseVo.serviceFail("用户信息查询失败");
            }
        } else {
            return ReponseVo.serviceFail("用户未登录");
        }
    }

    @RequestMapping(value = "updateUserInfo", method = RequestMethod.POST)
    public ReponseVo updateUserInfo(UserInfoModel userInfoModel) {
        //获取当前登录用户
        String userId = CurrentUser.getCurrentUserId();
        if (userId != null && userId.trim().length() > 0) {
            int uuid = Integer.parseInt(userId);
            //判断当前登录的id和需要修改的id是否一致
            if (uuid != userInfoModel.getUuid()) {
                return ReponseVo.serviceFail("请修改您自己的信息");
            }
            UserInfoModel userInfo = userAPI.updateUserInfo(userInfoModel);
            if (userInfo != null) {
                return ReponseVo.success(userInfo);
            } else {
                return ReponseVo.serviceFail("用户信息修改失败");
            }
        } else {
            return ReponseVo.serviceFail("用户未登录");
        }
    }
}
